import 'package:clean_app/asset/color.dart';
import 'package:clean_app/asset/modal.dart';
import 'package:clean_app/asset/storage.dart';
import 'package:clean_app/asset/textstyle.dart';
import 'package:clean_app/interface/mid_interface.dart';
import 'package:clean_app/provider/login_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class RegisterWithPwd extends StatefulWidget {
  const RegisterWithPwd({super.key});

  @override
  State<RegisterWithPwd> createState() => _RegisterWithPwdState();
}

class _RegisterWithPwdState extends State<RegisterWithPwd> {
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();
  bool pwdVisible = false;
  String role = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: os_back,
      appBar: AppBar(
        backgroundColor: os_back,
        leading: IconButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          icon: Icon(Icons.chevron_left),
        ),
        title: Text(
          "注册",
          style: XSTextStyle(
            context: context,
            color: os_light_dark_card,
            fontSize: 15,
            fontWeight: FontWeight.normal,
          ),
        ),
        centerTitle: true,
      ),
      body: ListView(
        padding: EdgeInsets.only(left: 20, right: 20),
        children: [
          Container(
            decoration: BoxDecoration(
              color: os_white,
              borderRadius: BorderRadius.circular(15),
            ),
            padding: EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
            child: Column(
              children: [
                TextField(
                  controller: _phoneController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: os_white,
                      ),
                    ),
                    hintText: "请输入用户名",
                    prefixIcon: Icon(Icons.person),
                  ),
                ),
                Container(height: 10),
                TextField(
                  controller: _pwdController,
                  obscureText: !pwdVisible,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: os_white,
                      ),
                    ),
                    hintText: "请输入密码",
                    prefixIcon: Icon(Icons.lock),
                    suffixIcon: Container(
                      padding: EdgeInsets.only(right: 5),
                      child: IconButton(
                        icon: Icon(pwdVisible
                            ? Icons.visibility_off_rounded
                            : Icons.remove_red_eye),
                        onPressed: () {
                          setState(() {
                            pwdVisible = !pwdVisible;
                          });
                        },
                      ),
                    ),
                  ),
                ),
                Container(height: 10),
                DropdownButtonFormField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: BorderSide(
                        color: os_white,
                      ),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: BorderSide(
                        color: os_white,
                      ),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: os_white,
                      ),
                    ),
                    hintText: "请选择角色",
                    prefixIcon: Icon(Icons.people),
                  ),
                  items: [
                    // DropdownMenuItem(
                    //   child: Text("超级管理员"),
                    //   value: "超级管理员",
                    // ),
                    DropdownMenuItem(
                      child: Text("保洁人员"),
                      value: "保洁人员",
                    ),
                    DropdownMenuItem(
                      child: Text("用户"),
                      value: "用户",
                    ),
                  ],
                  onChanged: (value) {
                    setState(() {
                      role = value!;
                    });
                  },
                ),
              ],
            ),
          ),
          Container(height: 50),
          ElevatedButton(
            style: ButtonStyle(
              backgroundColor: WidgetStatePropertyAll(os_white),
              elevation: WidgetStatePropertyAll(0),
              padding: WidgetStatePropertyAll(
                EdgeInsets.only(
                  top: 15,
                  bottom: 15,
                ),
              ),
            ),
            onPressed: () async {
              if (_phoneController.text.trim().isEmpty ||
                  _pwdController.text.trim().isEmpty ||
                  role.isEmpty) {
                Toast.showInCenter(text: "请填写完整信息", context: context);
                return;
              }

              var res = await AuthApi().authRegister({
                "username": _phoneController.text,
                "password": _pwdController.text,
                "role": role,
              });

              try {
                if (res.toString().contains("注册成功")) {
                  Toast.showInCenter(text: "注册成功", context: context);
                  await Future.delayed(Duration(milliseconds: 500));
                  Navigator.of(context).pop();
                } else {
                  Toast.showInCenter(text: res["message"], context: context);
                }
              } catch (_) {}
              print(res);
            },
            child: Text(
              "注册",
              style: XSTextStyle(
                context: context,
                color: os_color,
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
